/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package logica.jpaControllers;

import Entidades.Tipodocumento;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
import javax.persistence.Query;
import javax.persistence.EntityNotFoundException;
import Entidades.Operario;
import java.util.ArrayList;
import java.util.Collection;
import Entidades.Transportista;
import logica.jpaControllers.exceptions.IllegalOrphanException;
import logica.jpaControllers.exceptions.NonexistentEntityException;

/**
 *
 * @author Anali
 */
public class TipodocumentoJpaController {

    public TipodocumentoJpaController() {
        emf = Persistence.createEntityManagerFactory("GestorPesajeJpaPU2");
    }
    private EntityManagerFactory emf = null;

    public EntityManager getEntityManager() {
        return emf.createEntityManager();
    }

    public void create(Tipodocumento tipodocumento) {
        if (tipodocumento.getOperarioCollection() == null) {
            tipodocumento.setOperarioCollection(new ArrayList<Operario>());
        }
        if (tipodocumento.getTransportistaCollection() == null) {
            tipodocumento.setTransportistaCollection(new ArrayList<Transportista>());
        }
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            Collection<Operario> attachedOperarioCollection = new ArrayList<Operario>();
            for (Operario operarioCollectionOperarioToAttach : tipodocumento.getOperarioCollection()) {
                operarioCollectionOperarioToAttach = em.getReference(operarioCollectionOperarioToAttach.getClass(), operarioCollectionOperarioToAttach.getIdoperario());
                attachedOperarioCollection.add(operarioCollectionOperarioToAttach);
            }
            tipodocumento.setOperarioCollection(attachedOperarioCollection);
            Collection<Transportista> attachedTransportistaCollection = new ArrayList<Transportista>();
            for (Transportista transportistaCollectionTransportistaToAttach : tipodocumento.getTransportistaCollection()) {
                transportistaCollectionTransportistaToAttach = em.getReference(transportistaCollectionTransportistaToAttach.getClass(), transportistaCollectionTransportistaToAttach.getIdTransportista());
                attachedTransportistaCollection.add(transportistaCollectionTransportistaToAttach);
            }
            tipodocumento.setTransportistaCollection(attachedTransportistaCollection);
            em.persist(tipodocumento);
            for (Operario operarioCollectionOperario : tipodocumento.getOperarioCollection()) {
                Tipodocumento oldTipodocumentoOfOperarioCollectionOperario = operarioCollectionOperario.getTipodocumento();
                operarioCollectionOperario.setTipodocumento(tipodocumento);
                operarioCollectionOperario = em.merge(operarioCollectionOperario);
                if (oldTipodocumentoOfOperarioCollectionOperario != null) {
                    oldTipodocumentoOfOperarioCollectionOperario.getOperarioCollection().remove(operarioCollectionOperario);
                    oldTipodocumentoOfOperarioCollectionOperario = em.merge(oldTipodocumentoOfOperarioCollectionOperario);
                }
            }
            for (Transportista transportistaCollectionTransportista : tipodocumento.getTransportistaCollection()) {
                Tipodocumento oldTipodocumentoOfTransportistaCollectionTransportista = transportistaCollectionTransportista.getTipodocumento();
                transportistaCollectionTransportista.setTipodocumento(tipodocumento);
                transportistaCollectionTransportista = em.merge(transportistaCollectionTransportista);
                if (oldTipodocumentoOfTransportistaCollectionTransportista != null) {
                    oldTipodocumentoOfTransportistaCollectionTransportista.getTransportistaCollection().remove(transportistaCollectionTransportista);
                    oldTipodocumentoOfTransportistaCollectionTransportista = em.merge(oldTipodocumentoOfTransportistaCollectionTransportista);
                }
            }
            em.getTransaction().commit();
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public void edit(Tipodocumento tipodocumento) throws IllegalOrphanException, NonexistentEntityException, Exception {
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            Tipodocumento persistentTipodocumento = em.find(Tipodocumento.class, tipodocumento.getIdtipodocumento());
            Collection<Operario> operarioCollectionOld = persistentTipodocumento.getOperarioCollection();
            Collection<Operario> operarioCollectionNew = tipodocumento.getOperarioCollection();
            Collection<Transportista> transportistaCollectionOld = persistentTipodocumento.getTransportistaCollection();
            Collection<Transportista> transportistaCollectionNew = tipodocumento.getTransportistaCollection();
            List<String> illegalOrphanMessages = null;
            for (Operario operarioCollectionOldOperario : operarioCollectionOld) {
                if (!operarioCollectionNew.contains(operarioCollectionOldOperario)) {
                    if (illegalOrphanMessages == null) {
                        illegalOrphanMessages = new ArrayList<String>();
                    }
                    illegalOrphanMessages.add("You must retain Operario " + operarioCollectionOldOperario + " since its tipodocumento field is not nullable.");
                }
            }
            for (Transportista transportistaCollectionOldTransportista : transportistaCollectionOld) {
                if (!transportistaCollectionNew.contains(transportistaCollectionOldTransportista)) {
                    if (illegalOrphanMessages == null) {
                        illegalOrphanMessages = new ArrayList<String>();
                    }
                    illegalOrphanMessages.add("You must retain Transportista " + transportistaCollectionOldTransportista + " since its tipodocumento field is not nullable.");
                }
            }
            if (illegalOrphanMessages != null) {
                throw new IllegalOrphanException(illegalOrphanMessages);
            }
            Collection<Operario> attachedOperarioCollectionNew = new ArrayList<Operario>();
            for (Operario operarioCollectionNewOperarioToAttach : operarioCollectionNew) {
                operarioCollectionNewOperarioToAttach = em.getReference(operarioCollectionNewOperarioToAttach.getClass(), operarioCollectionNewOperarioToAttach.getIdoperario());
                attachedOperarioCollectionNew.add(operarioCollectionNewOperarioToAttach);
            }
            operarioCollectionNew = attachedOperarioCollectionNew;
            tipodocumento.setOperarioCollection(operarioCollectionNew);
            Collection<Transportista> attachedTransportistaCollectionNew = new ArrayList<Transportista>();
            for (Transportista transportistaCollectionNewTransportistaToAttach : transportistaCollectionNew) {
                transportistaCollectionNewTransportistaToAttach = em.getReference(transportistaCollectionNewTransportistaToAttach.getClass(), transportistaCollectionNewTransportistaToAttach.getIdTransportista());
                attachedTransportistaCollectionNew.add(transportistaCollectionNewTransportistaToAttach);
            }
            transportistaCollectionNew = attachedTransportistaCollectionNew;
            tipodocumento.setTransportistaCollection(transportistaCollectionNew);
            tipodocumento = em.merge(tipodocumento);
            for (Operario operarioCollectionNewOperario : operarioCollectionNew) {
                if (!operarioCollectionOld.contains(operarioCollectionNewOperario)) {
                    Tipodocumento oldTipodocumentoOfOperarioCollectionNewOperario = operarioCollectionNewOperario.getTipodocumento();
                    operarioCollectionNewOperario.setTipodocumento(tipodocumento);
                    operarioCollectionNewOperario = em.merge(operarioCollectionNewOperario);
                    if (oldTipodocumentoOfOperarioCollectionNewOperario != null && !oldTipodocumentoOfOperarioCollectionNewOperario.equals(tipodocumento)) {
                        oldTipodocumentoOfOperarioCollectionNewOperario.getOperarioCollection().remove(operarioCollectionNewOperario);
                        oldTipodocumentoOfOperarioCollectionNewOperario = em.merge(oldTipodocumentoOfOperarioCollectionNewOperario);
                    }
                }
            }
            for (Transportista transportistaCollectionNewTransportista : transportistaCollectionNew) {
                if (!transportistaCollectionOld.contains(transportistaCollectionNewTransportista)) {
                    Tipodocumento oldTipodocumentoOfTransportistaCollectionNewTransportista = transportistaCollectionNewTransportista.getTipodocumento();
                    transportistaCollectionNewTransportista.setTipodocumento(tipodocumento);
                    transportistaCollectionNewTransportista = em.merge(transportistaCollectionNewTransportista);
                    if (oldTipodocumentoOfTransportistaCollectionNewTransportista != null && !oldTipodocumentoOfTransportistaCollectionNewTransportista.equals(tipodocumento)) {
                        oldTipodocumentoOfTransportistaCollectionNewTransportista.getTransportistaCollection().remove(transportistaCollectionNewTransportista);
                        oldTipodocumentoOfTransportistaCollectionNewTransportista = em.merge(oldTipodocumentoOfTransportistaCollectionNewTransportista);
                    }
                }
            }
            em.getTransaction().commit();
        } catch (Exception ex) {
            String msg = ex.getLocalizedMessage();
            if (msg == null || msg.length() == 0) {
                Integer id = tipodocumento.getIdtipodocumento();
                if (findTipodocumento(id) == null) {
                    throw new NonexistentEntityException("The tipodocumento with id " + id + " no longer exists.");
                }
            }
            throw ex;
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public void destroy(Integer id) throws IllegalOrphanException, NonexistentEntityException {
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            Tipodocumento tipodocumento;
            try {
                tipodocumento = em.getReference(Tipodocumento.class, id);
                tipodocumento.getIdtipodocumento();
            } catch (EntityNotFoundException enfe) {
                throw new NonexistentEntityException("The tipodocumento with id " + id + " no longer exists.", enfe);
            }
            List<String> illegalOrphanMessages = null;
            Collection<Operario> operarioCollectionOrphanCheck = tipodocumento.getOperarioCollection();
            for (Operario operarioCollectionOrphanCheckOperario : operarioCollectionOrphanCheck) {
                if (illegalOrphanMessages == null) {
                    illegalOrphanMessages = new ArrayList<String>();
                }
                illegalOrphanMessages.add("This Tipodocumento (" + tipodocumento + ") cannot be destroyed since the Operario " + operarioCollectionOrphanCheckOperario + " in its operarioCollection field has a non-nullable tipodocumento field.");
            }
            Collection<Transportista> transportistaCollectionOrphanCheck = tipodocumento.getTransportistaCollection();
            for (Transportista transportistaCollectionOrphanCheckTransportista : transportistaCollectionOrphanCheck) {
                if (illegalOrphanMessages == null) {
                    illegalOrphanMessages = new ArrayList<String>();
                }
                illegalOrphanMessages.add("This Tipodocumento (" + tipodocumento + ") cannot be destroyed since the Transportista " + transportistaCollectionOrphanCheckTransportista + " in its transportistaCollection field has a non-nullable tipodocumento field.");
            }
            if (illegalOrphanMessages != null) {
                throw new IllegalOrphanException(illegalOrphanMessages);
            }
            em.remove(tipodocumento);
            em.getTransaction().commit();
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public List<Tipodocumento> findTipodocumentoEntities() {
        return findTipodocumentoEntities(true, -1, -1);
    }

    public List<Tipodocumento> findTipodocumentoEntities(int maxResults, int firstResult) {
        return findTipodocumentoEntities(false, maxResults, firstResult);
    }

    private List<Tipodocumento> findTipodocumentoEntities(boolean all, int maxResults, int firstResult) {
        EntityManager em = getEntityManager();
        try {
            Query q = em.createQuery("select object(o) from Tipodocumento as o");
            if (!all) {
                q.setMaxResults(maxResults);
                q.setFirstResult(firstResult);
            }
            return q.getResultList();
        } finally {
            em.close();
        }
    }

    public Tipodocumento findTipodocumento(Integer id) {
        EntityManager em = getEntityManager();
        try {
            return em.find(Tipodocumento.class, id);
        } finally {
            em.close();
        }
    }

    public int getTipodocumentoCount() {
        EntityManager em = getEntityManager();
        try {
            Query q = em.createQuery("select count(o) from Tipodocumento as o");
            return ((Long) q.getSingleResult()).intValue();
        } finally {
            em.close();
        }
    }
 public Tipodocumento findTipodocumento_x_nombre(String nom) {
          EntityManager em = getEntityManager();
        try {
            Query q = em.createQuery("select u from Tipodocumento u where u.nombretipo = " + "'" + nom +"'");
            return (Tipodocumento) q.getSingleResult();
        } finally {
            em.close();
        }
    }
}
